﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using Microsoft.Win32;
using System.Security.Permissions;

namespace ProDeletor
{
    public partial class ProDeletor : ServiceBase
    {
        Timer timer;
        Controller controller;
        int minutes;
        string controlFile;

        public ProDeletor()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            EventLogWriter eventLog = new EventLogWriter();
            //controlFile = Properties.Settings.Default.ControlFile;
            RegistryKey registryKey = Registry.LocalMachine;
            RegistryPermission registryAccess = new RegistryPermission(
            RegistryPermissionAccess.Write | RegistryPermissionAccess.Read,
                @"HKEY_LOCAL_MACHINE\SOFTWARE\ProDeletor");
            RegistryKey localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Environment.Is64BitOperatingSystem ?
                    RegistryView.Registry64 : RegistryView.Registry32);
            
            try
            {
                //RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ProDeletor");
                RegistryKey regKey = localMachine.OpenSubKey(@"SOFTWARE\ProDeletor");
                controlFile = regKey.GetValue("ControlFile").ToString();
            }
            catch
            {
                eventLog.writeLogEntry("Cannot read from registry 'HKLM/SOFTWARE/ProDeletor' : " + controlFile + ", service cannot start.", EventLogEntryType.Error);
                this.Stop();
            }
            if (!File.Exists(controlFile))
            {
                eventLog.writeLogEntry("Controlfile '" + controlFile + "' cannot reach. Service cannot start.", EventLogEntryType.Error);
                this.Stop();
                
                
            }
            controller = new Controller(controlFile);
            controller.readControlFile();
            minutes = DateTime.Now.Minute-1;
            timer = new Timer();
            timer.Interval = 2000;
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Enabled = true;
            timer.Start();

        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (minutes != DateTime.Now.Minute)
            {
                minutes = DateTime.Now.Minute;
                controller.checkTasks();
            }
        }

        protected override void OnStop()
        {
            if (timer != null)
            {
                timer.Stop();
                timer.Enabled = false;
            }
            timer = null;
            controller = null;
            timer = null;
        }

        protected override void OnPause()
        {
            base.OnPause();
            timer.Stop();
            timer.Enabled = false;
        }

        protected override void OnContinue()
        {
            base.OnContinue();
            timer.Start();
            timer.Enabled = true;
            minutes = DateTime.Now.Minute - 1;
        }
    }
}
